package utlis

import (
	"github.com/google/uuid"
	capi "github.com/hashicorp/consul/api"
)

type ConsulClient struct {
	client *capi.Client
}

func NewClient(addr string) *ConsulClient {

	cfg := capi.DefaultConfig()
	cfg.Address = addr

	// Get a new client
	client, err := capi.NewClient(cfg)
	if err != nil {
		panic(err)
	}

	return &ConsulClient{client: client}

}

type GrpcConf struct {
	Name    string
	Tags    []string
	Post    int
	Address string
}

func (c *ConsulClient) RegisterServer(cfg GrpcConf) error {
	registration := capi.AgentServiceRegistration{

		ID:      uuid.NewString(),
		Name:    cfg.Name,
		Tags:    cfg.Tags,
		Port:    cfg.Post,
		Address: cfg.Address,
	}
	return c.client.Agent().ServiceRegister(&registration)
}
